﻿package mortal.game.view.mainUI.roleAvatar
{
    import Message.BroadCast.*;
    import Message.DB.Tables.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.component.gconst.*;
    import mortal.game.resource.*;

    public class BuffPanel extends GSprite
    {
        private var _showingBuffArray:Array;
        private var _iconGroupDic:Dictionary;
        private var _needToshowBuffDic:Dictionary;
        private var _buffDcit:Dictionary;
        private var _buffGroupDcit:Dictionary;
        private var _buffSetDicit:Dictionary;
        private var _buffIconGroupDic:Dictionary;
        private var _buffPanelBg:ScaleBitmap;
        private static var _buffItemPool:Array = [];

        public function BuffPanel()
        {
            this._showingBuffArray = [];
            this._iconGroupDic = new Dictionary();
            this._needToshowBuffDic = new Dictionary();
            this._buffDcit = new Dictionary();
            this._buffGroupDcit = new Dictionary();
            this._buffSetDicit = new Dictionary();
            this._buffIconGroupDic = new Dictionary();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._buffPanelBg = ResourceConst.getScaleBitmap(ImagesConst.CombatTextBg);
            this._buffPanelBg.setSize(17, 17);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._buffPanelBg.dispose();
            this._buffPanelBg = null;
            this.clearBuffs();
            this.clearDict();
            super.disposeImpl(param1);
            return;
        }// end function

        private function setVisibleBuffs() : void
        {
            var _loc_1:BuffItem = null;
            for each (_loc_1 in this._showingBuffArray)
            {
                
                _loc_1.buffData = null;
                _loc_1.visible = false;
            }
            return;
        }// end function

        private function clearBuffs() : void
        {
            var _loc_1:BuffItem = null;
            for each (_loc_1 in this._showingBuffArray)
            {
                
                DisplayUtil.removeMe(_loc_1);
                _buffItemPool.push(_loc_1);
            }
            this._showingBuffArray.length = 0;
            return;
        }// end function

        private function cheakBuffHandler(param1:BuffData) : void
        {
            var _loc_3:BuffData = null;
            var _loc_2:* = param1.tbuff;
            if (_loc_2 && _loc_2.icon != 0)
            {
                if (this._buffDcit[_loc_2.buffId] == null && this._buffGroupDcit[_loc_2.group] == null && this._buffSetDicit[_loc_2.set + "_" + _loc_2.type] == null)
                {
                    this._needToshowBuffDic[_loc_2.buffId] = param1;
                }
                if (_loc_2.group > 0)
                {
                    this._buffGroupDcit[_loc_2.group] = param1;
                }
                else
                {
                    this._buffDcit[_loc_2.buffId] = param1;
                }
                if (_loc_2.set > 0)
                {
                    _loc_3 = this._buffSetDicit[_loc_2.set + "_" + _loc_2.type];
                    if (_loc_3)
                    {
                        if (_loc_3.tbuff.effect <= _loc_2.effect)
                        {
                            if (this._needToshowBuffDic[_loc_3.tbuff.buffId])
                            {
                                delete this._needToshowBuffDic[_loc_3.tbuff.buffId];
                                this._needToshowBuffDic[_loc_2.buffId] = param1;
                            }
                            this._buffSetDicit[_loc_2.set + "_" + _loc_2.type] = param1;
                        }
                    }
                    else
                    {
                        this._buffSetDicit[_loc_2.set + "_" + _loc_2.type] = param1;
                    }
                }
                else
                {
                    this._buffDcit[_loc_2.buffId] = param1;
                }
            }
            return;
        }// end function

        public function updateSelfeBuffer(param1:Array) : void
        {
            var _loc_2:BuffData = null;
            var _loc_3:TBuff = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            this.clearDict();
            if (param1 && param1.length > 0)
            {
                _loc_4 = param1.length;
                _loc_5 = 0;
                while (_loc_5 < _loc_4)
                {
                    
                    _loc_2 = param1[_loc_5] as BuffData;
                    _loc_3 = _loc_2.tbuff;
                    if (_loc_3)
                    {
                        this.cheakBuffHandler(_loc_2);
                    }
                    _loc_5++;
                }
            }
            this.createBufferItem();
            return;
        }// end function

        public function updateEntityBuffer(param1:Array) : void
        {
            var _loc_2:BuffData = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            this.clearDict();
            if (param1 && param1.length > 0)
            {
                _loc_3 = param1.length;
                _loc_4 = 0;
                while (_loc_4 < _loc_3)
                {
                    
                    _loc_2 = new BuffData((param1[_loc_4] as SBuffInfo).buffId);
                    this.cheakBuffHandler(_loc_2);
                    _loc_4++;
                }
            }
            this.createBufferItem();
            return;
        }// end function

        private function getBuffItem() : BuffItem
        {
            var _loc_1:BuffItem = null;
            if (_buffItemPool.length)
            {
                _loc_1 = _buffItemPool.pop();
            }
            else
            {
                _loc_1 = new BuffItem();
                _loc_1.bg = this._buffPanelBg.bitmapData;
            }
            return _loc_1;
        }// end function

        private function addBuffItems(param1:Dictionary) : void
        {
            var _loc_2:BuffData = null;
            var _loc_3:BuffItem = null;
            var _loc_5:BuffData = null;
            var _loc_4:int = 0;
            for each (_loc_5 in param1)
            {
                
                _loc_3 = null;
                if (this._buffGroupDcit[_loc_5.tbuff.iconGroup] == null)
                {
                    _loc_3 = this._showingBuffArray[_loc_4];
                    if (_loc_3 == null)
                    {
                        _loc_3 = this.getBuffItem();
                        _loc_3.x = _loc_4 % 5 * 20;
                        _loc_3.y = Math.floor(_loc_4 / 5) * 20;
                        this.addChild(_loc_3);
                        this._showingBuffArray.push(_loc_3);
                    }
                    _loc_3.visible = true;
                    if (_loc_3.buffData == null || _loc_3.buffData.buffId != _loc_5.buffId)
                    {
                        _loc_3.buffData = _loc_5;
                    }
                    _loc_4++;
                }
                if (_loc_5.tbuff.iconGroup > 0)
                {
                    this._buffGroupDcit[_loc_5.tbuff.iconGroup] = _loc_5;
                    if (this._iconGroupDic[_loc_5.tbuff.iconGroup] == null)
                    {
                        this._iconGroupDic[_loc_5.tbuff.iconGroup] = new Array();
                    }
                    (this._iconGroupDic[_loc_5.tbuff.iconGroup] as Array).push(_loc_5);
                    if (_loc_3 && _loc_3.buffData)
                    {
                        _loc_3.buffData.iconGroupList = this._iconGroupDic[_loc_5.tbuff.iconGroup] as Array;
                    }
                }
            }
            this.clearDict();
            return;
        }// end function

        private function createBufferItem() : void
        {
            this.setVisibleBuffs();
            this._iconGroupDic = new Dictionary();
            this.addBuffItems(this._needToshowBuffDic);
            return;
        }// end function

        private function clearDict() : void
        {
            this._buffDcit = new Dictionary();
            this._buffGroupDcit = new Dictionary();
            this._buffSetDicit = new Dictionary();
            this._buffIconGroupDic = new Dictionary();
            this._needToshowBuffDic = new Dictionary();
            return;
        }// end function

        public function clean() : void
        {
            this.clearBuffs();
            this.clearDict();
            return;
        }// end function

    }
}
